package com.hkd.system.service.question.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.hkd.common.core.constants.Constants;
import com.hkd.common.core.domain.R;
import com.hkd.common.core.domain.TableDataInfo;
import com.hkd.common.core.enums.ResultCode;
import com.hkd.system.domain.question.Question;
import com.hkd.system.domain.question.dto.QuestionAddDTO;
import com.hkd.system.domain.question.dto.QuestionEditDTO;
import com.hkd.system.domain.question.dto.QuestionQueryDTO;
import com.hkd.system.domain.question.vo.QuestionDetailVO;
import com.hkd.system.domain.question.vo.QuestionVO;
import com.hkd.system.mapper.question.QuestionMapper;
import com.hkd.system.service.question.IQuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.hkd.common.core.controller.BaseController.getTableDataInfo;


@Slf4j
@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public TableDataInfo list(QuestionQueryDTO questionQueryDTO) {
        //排除的题目id集合
        String excludeIdStr = questionQueryDTO.getExcludeIdStr();
        if(StrUtil.isNotEmpty(excludeIdStr)){
            String[] excludeIdArr = excludeIdStr.split(Constants.SPLIT_SEM);
            //类型和去重
            Set<Long> excludeIdSet = Arrays.stream(excludeIdArr)
                    .map(Long::valueOf).collect(Collectors.toSet());
            questionQueryDTO.setExcludeIdSet(excludeIdSet);
        }
        //分页查询
        PageHelper.startPage(questionQueryDTO.getPageNum(), questionQueryDTO.getPageSize());
        //查询数据
        List<QuestionVO> questionVOList = questionMapper.selectQuestionList(questionQueryDTO);

        return getTableDataInfo(questionVOList);
    }

    @Override
    public R add(QuestionAddDTO questionAddDTO) {
        //参数校验
        if(questionAddDTO == null){
            log.warn(ResultCode.FAILED_QUESTION_AddDTO.getMessage());
            return R.failed(ResultCode.FAILED_QUESTION_AddDTO);
        }

        //参数转换
        Question question = new Question();
        BeanUtil.copyProperties(questionAddDTO,question);

        //重复校验
        List<Question> questionsList = questionMapper.selectList(new LambdaQueryWrapper<Question>().eq(Question::getTitle, question.getTitle()));

        if(!CollectionUtils.isEmpty(questionsList)){
            log.warn(ResultCode.FAILED_QUESTION_HAS_EXITS.getMessage());
            return R.failed(ResultCode.FAILED_QUESTION_HAS_EXITS);
        }
        //添加数据
        int insert = questionMapper.insert(question);
        if(insert <= 0){
            //添加失败
            log.error(ResultCode.FAILED_QUESTION_ADD.getMessage());
            return R.failed(ResultCode.FAILED_QUESTION_ADD);
        }
        return R.success();
    }

    @Override
    public R detail(Long questionId) {
        //参数校验
        if(questionId == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            return R.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //查询数据
        Question question = questionMapper.selectById(questionId);
        if(question == null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            return R.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        BeanUtil.copyProperties(question,questionDetailVO);

        return R.success(questionDetailVO);
    }

    @Override
    public R edit(QuestionEditDTO questionEditDTO) {
        log.info("questionID: {}",questionEditDTO.getQuestionId());
        Question oldQuestion = questionMapper.selectById(questionEditDTO.getQuestionId());
        if(oldQuestion == null){
            log.warn(ResultCode.FAILED_QUESTION_EXIT.getMessage());
            return R.failed(ResultCode.FAILED_QUESTION_EXIT);
        }
        //创建update对象
        Question updateQuestion = new Question();
        updateQuestion.setQuestionId(questionEditDTO.getQuestionId());
        updateQuestion.setTitle(questionEditDTO.getTitle());
        updateQuestion.setDifficulty(questionEditDTO.getDifficulty());
        updateQuestion.setTimeLimit(questionEditDTO.getTimeLimit());
        updateQuestion.setSpaceLimit(questionEditDTO.getSpaceLimit());
        updateQuestion.setContent(questionEditDTO.getContent());
        updateQuestion.setQuestionCase(questionEditDTO.getQuestionCase());
        updateQuestion.setDefaultCode(questionEditDTO.getDefaultCode());
        updateQuestion.setMainFunc(questionEditDTO.getMainFunc());

        //更新数据
        int update = questionMapper.updateById(updateQuestion);
        if(update <= 0){
            log.error(ResultCode.FAILED_QUESTION_EXIT_UPDATE.getMessage());
            return R.failed(ResultCode.FAILED_QUESTION_EXIT_UPDATE);
        }
        return R.success();
    }

    @Override
    public R delete(Long questionId) {
        //参数校验
        if(questionId == null || questionId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            return R.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //检验数据是否存在
        Question question = questionMapper.selectById(questionId);
        if(question == null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            return R.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        int i = questionMapper.deleteById(questionId);
        if(i <= 0){
            log.error(ResultCode.FAILED_QUESTION_DELETE.getMessage());
            return R.failed(ResultCode.FAILED_QUESTION_DELETE);
        }
        return R.success();
    }
}

